/*
 * Copyright 2008 Nathan C Jones
 * 
 * This file is part of Fivehundred.
 * 
 * Fivehundred is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * Fivehundred is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * Fivehundred. If not, see <http://www.gnu.org/licenses/>. 
 */

package com.googlecode.fivehundred.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * A sorted collection of cards which a player holds. Cards added to the hand
 * will be added at a position according to its natural ordering.
 * 
 * @author Nathan C Jones
 */
public class Hand extends AbstractCardCollection {

	/** The cards in the hand. */
	SortedSet<Card> cards;

	/**
	 * Create a new empty card hand.
	 */
	public Hand() {
		cards = new TreeSet<Card>();
	}

	/**
	 * Take a card out of the hand at the given index. The card will be removed
	 * from the hand.
	 * 
	 * @param index the number of the card within the collection to retrieve.
	 * @return the card at the given index.
	 * @throws IndexOutOfBoundsException if there is no card at the given index.
	 */
	public synchronized Card takeCard(Integer index) {
		ArrayList<Card> cardList = new ArrayList<Card>(cards);
		Card card = cardList.get(index);
		cards.remove(card);
		fireCardRemoved(card);
		return card;
	}

	@Override
	protected Collection<Card> getCards() {
		return cards;
	}
}
